package com.example.basecommon.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;

import java.io.IOException;

/**
 * @author：dlj
 * @data：2022/1/11
 * @des：
 */
public class MusicUtils {
    private static AssetManager assetManager;
    private MediaPlayer player;

    //单例对象
    private static MusicUtils instance = null;

    static {
        instance = new MusicUtils();
    }

    //静态工厂方法
    public static MusicUtils getInstance() {
        return instance;
    }

    /**
     * @param speakType 1 成功提示音1  2 成功提示音2  3 错误提示音
     * @return
     */
    public void playSuccess(int speakType) {
        try {
            if (player != null){
                player.stop();
                player.release();
                player = null;
            }
            AssetFileDescriptor fileDescriptor1;
            player = new MediaPlayer();
            assetManager = App.INSTANCE.getApplicationContext().getAssets();
            switch (speakType) {
                case 1:
                    fileDescriptor1 = assetManager.openFd("Finish.wav");
                    break;
                case 2:
                    fileDescriptor1 = assetManager.openFd("success2.mp3");
                    break;
                case 3:
                    fileDescriptor1 = assetManager.openFd("Error.wav");
                    break;
                default:
                    fileDescriptor1 = assetManager.openFd("Finish.wav");
                    break;
            }
            player.setDataSource(fileDescriptor1.getFileDescriptor(), fileDescriptor1.getStartOffset(),
                    fileDescriptor1.getStartOffset());
            player.prepare();
            player.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
